package com.mql.onlinechatsystem.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mql.onlinechatsystem.system.entity.TbMessage;
import com.mql.onlinechatsystem.system.entity.TbMessagerelation;
import com.mql.onlinechatsystem.system.mapper.TbMessageMapper;
import com.mql.onlinechatsystem.system.mapper.TbMessagerelationMapper;
import com.mql.onlinechatsystem.system.service.ITbMessagerelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mql.onlinechatsystem.system.vo.TbMessagerelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马庆林
 * @since 2023-03-08
 */
@Service
public class TbMessagerelationServiceImpl extends ServiceImpl<TbMessagerelationMapper, TbMessagerelation> implements ITbMessagerelationService {

    @Autowired
    private TbMessageMapper tbMessageMapper;
    @Override
    public List<TbMessagerelationVo> getMessageList(TbMessagerelation tbMessagerelation) {

        List<TbMessagerelation> tbMessagerelations = baseMapper.selectList(new QueryWrapper<TbMessagerelation>().in("fromuserid", tbMessagerelation.getFromuserid(), tbMessagerelation.getTouserid())
                .in("touserid", tbMessagerelation.getFromuserid(), tbMessagerelation.getTouserid()));
        List<TbMessagerelationVo> collect = tbMessagerelations.stream().map(item -> {
            TbMessagerelationVo tbMessagerelationVo = new TbMessagerelationVo();
            BeanUtils.copyProperties(item, tbMessagerelationVo);
            TbMessage messageid = tbMessageMapper.selectOne(new QueryWrapper<TbMessage>().eq("messageid", item.getMessageid()));
            tbMessagerelationVo.setMessage(messageid.getMessage());
            return tbMessagerelationVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
